package function;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import entity.Operator;
import interfaces.OperatorDao;

import java.sql.Connection;
import java.util.List;

public class OperatorDaoImpl extends BaseDao2<Operator> implements OperatorDao {
    private static  OperatorDaoImpl operatorDao=null;
    private OperatorDaoImpl(){

    }
    public static OperatorDaoImpl getOperatorDao(){
        if(operatorDao==null)
            operatorDao=new OperatorDaoImpl();
        return operatorDao;
    }
    @Override
    public boolean insertOperator(Connection conn, Operator operator) {
        String sql="insert into operator(name,password,manager_id) values(?,?,?)";
        int update = 0;
        try {
            update = update(conn, sql, operator.getName(), operator.getPassword(), operator.getManagerId());
        } catch (Exception e) {
            System.out.println("该账户名已存在");
        }
        if(update==0)
        return false;
        else
            return true;
    }

    @Override
    public boolean deleteOperator(Connection conn, int id) {
       String sql="delete from operator where id=?";
        int update = 0;
        try {
            update = update(conn, sql, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(update==0)
            return false;
        else
            return true;
    }

    @Override
    public boolean updateOperator(Connection conn, int id, Operator operator) {
        String sql="update operator set name=?,password=?,manager_id=? where id=?";
        int update=0;
        try {
            update = update(conn, sql, operator.getName(), operator.getPassword(),operator.getManagerId(), operator.getId());
        } catch (MySQLIntegrityConstraintViolationException throwables) {
            System.out.println("用户名已存在");
        }
        if(update==0)
            return false;
        else
            return true;
    }

    @Override
    public Operator selectById(Connection conn, int id) {
        String sql="select o.id id,o.name name,o.password password,manager_id managerId, m.name managerName from operator o left join manager m on manager_id=m.id where o.id=?";
        Operator instance = null;
        try {
            instance = getInstance(conn, sql, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return instance;
    }

    @Override
    public List<Operator> selectByManager(Connection conn, int ManagerId) {
      String sql=  "select o.id id,o.name name,o.password password,manager_id managerId,m.name managerName from operator o left join manager m on manager_id=m.id where manager_id=?";
        List<Operator> forList=null;
      try {
             forList = getForList(conn, sql, ManagerId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;
    }

    @Override
    public List<Operator> selectAll(Connection conn) {
       String sql="select o.id id,o.name name,o.password password,manager_id managerId,m.name managerName from operator o left join manager m on o.manager_id=m.id ";
        List<Operator> forList = null;
        try {
          forList=getForList(conn,sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forList;
    }

    @Override
    public Operator SelectByName(Connection conn, String name) {
        String sql="select id id,name name,password password,manager_id managerId from operator where name=?";
        Operator operator = getInstance(conn, sql, name);
        return operator;
    }
}
